package com.cn.wanxi.util;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class MyImgLoad {
    private static String imgurl = "/images/";

    public static void load(HttpServletRequest req) throws UnsupportedEncodingException {
        FileItemFactory factory = new DiskFileItemFactory();

        // 文件上传处理器
        ServletFileUpload upload = new ServletFileUpload(factory);

        // 解析请求信息
        List<FileItem> items = null;
        try {
            items = upload.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        // 对请求信息进行判断
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            // 信息为普通的格式
            if (item.isFormField()) {
                String fieldName = item.getFieldName();//得到id值
                String value = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");//得到value值
                req.setAttribute(fieldName, value);
            }
            // 信息为文件格式
            else {
                //c:/caadfa/fsdad.jpg
                String fileName = item.getName();//获得上传图片的名称
                int index = fileName.lastIndexOf("\\");

                fileName = fileName.substring(index + 1);
                fileName = UUID.randomUUID().toString() + "--" + fileName;


                String basePath = req.getSession().getServletContext().getRealPath(imgurl);
                req.setAttribute("imgHref", imgurl + fileName);
                System.out.println(basePath);//打印当前位置
                req.setAttribute("wangEditorUrl", imgurl + fileName);
                File file = new File(basePath, fileName);
                try {

                    item.write(file);
                    saveFile(file, fileName);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void saveFile(File file, String fileName) {
        try {
            InputStream inputStream = new FileInputStream(file);
            String path = "E:\\wanxi\\step-four\\food04-1012/web/" + imgurl;
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            System.out.println(path);
            File createFile = new File(tempFile, fileName);
            if (!createFile.exists()) {
                createFile.createNewFile();
            }
            OutputStream outputStream = new FileOutputStream(createFile);
            int result;
            while ((result = inputStream.read()) != -1) {
                outputStream.write(result);
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
